﻿using System.Text.RegularExpressions;

namespace Tools.Utilities
{
    public static class TagReplacer
    {
        public static string PrepareHtml(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                str = ReplaceDiv(str);
                string aaa =
                    Regex.Replace(
                        str.Replace("<em>&nbsp;</em>", "&nbsp;").Replace("<strong>&nbsp;</strong>", "&nbsp;").Replace(
                            "<div>&nbsp;</div>", "<br/><br/>").Replace("...", "&hellip;"),
                        @"<br\s*/>",
                        "<br>").Replace("<div>", string.Empty);
                aaa = Regex.Replace(
                    aaa, @"<div\s+align\s*=\s*[^<>]*center[^<>]*\s*>(?<inner>.*?)</div>", "<center>${inner}</center>");
                return string.IsNullOrEmpty(str)
                           ? string.Empty
                           : Regex.Replace(
                               Regex.Replace(
                                   aaa.Replace("</div>", /*"</font>"*/string.Empty), "class=\".*?\"", string.Empty),
                               "<div.*?>",
                               string.Empty);
            }

            return string.Empty;
        }

        public static string ReplaceDiv(string inputString)
        {
            inputString = ReplaceCenteredDivs(inputString);

            inputString = Regex.Replace(inputString, "<div>&nbsp;", string.Empty);
            inputString = Regex.Replace(inputString, "<div>[\r\n]*", string.Empty);
            inputString = Regex.Replace(inputString, "<div.*?>", string.Empty);
            inputString = Regex.Replace(inputString, "</div>", "<br>");

            inputString = Regex.Replace(inputString, "(<br>[\r\n]*)*$", string.Empty);

            return inputString;
        }

        public static string ReplaceCenteredDivs(string html)
        {
            if (string.IsNullOrEmpty(html))
            {
                return html;
            }

            var matches = Regex.Matches(
                html,
                @"<div\s+((align\s*=\s*[^<>]*center[^<>]*\s*)|(style\s*=[^<>]*text-align\s*:\s*center[^<>]*))>",
                RegexOptions.Compiled);
            int initialHtmlLen = html.Length;
            int offset = 0;
            const string ClosingDiv = "</div>";
            foreach (Match match in matches)
            {
                html = ReplaceFirstOccurrence(html, match.Value, "<center>");
                int openCloseCounter = 1;
                int startIndex = match.Index - offset + 1;
                offset = initialHtmlLen - html.Length;

                while (startIndex < html.Length)
                {
                    int nextOpenDiv = html.IndexOf("<div", startIndex);
                    int nextClosingDiv = html.IndexOf(ClosingDiv, startIndex);
                    if (nextClosingDiv < 1)
                    {
                        break;
                    }

                    if (nextClosingDiv < nextOpenDiv || nextOpenDiv < 0)
                    {
                        openCloseCounter--;
                        startIndex = nextClosingDiv + 1;
                        if (openCloseCounter == 0)
                        {
                            html = html.Substring(0, nextClosingDiv) + "</center>" +
                                   html.Substring(nextClosingDiv + ClosingDiv.Length);
                            break;
                        }
                    }
                    else
                    {
                        openCloseCounter++;
                        startIndex = nextOpenDiv + 1;
                    }
                }
            }

            return html;
        }

        public static string Replace(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }

            str = ReplaceCenteredDivs(str);
            str = str.Replace("<div>&nbsp;", string.Empty);
            str = Regex.Replace(str, "<div.*?>[\r\n]*", string.Empty);
            str = str.Replace("</div>", "<br>");
            str = str.Replace("<em>&nbsp;</em>", "&nbsp;");
            str = str.Replace("<strong>&nbsp;</strong>", "&nbsp;");
            str = str.Replace("...", "&hellip;");
            str = Regex.Replace(str, "class=\".*?\"", string.Empty);
            str = Regex.Replace(str, @"<br\s*/>", "<br>");
            if (str.EndsWith("<br>"))
            {
                str = str.Substring(0, str.Length - 4);
            }

            return str;
        }

        private static string ReplaceFirstOccurrence(string original, string oldValue, string newValue)
        {
            if (string.IsNullOrEmpty(original))
            {
                return string.Empty;
            }

            if (string.IsNullOrEmpty(oldValue))
            {
                return original;
            }

            if (string.IsNullOrEmpty(newValue))
            {
                newValue = string.Empty;
            }

            int loc = original.IndexOf(oldValue);
            return original.Remove(loc, oldValue.Length).Insert(loc, newValue);
        }
    }
}
